package com.gok.seasonmovie.controller;

import com.gok.seasonmovie.entity.Comment;
import com.gok.seasonmovie.entity.CommentDTO;
import com.gok.seasonmovie.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping("/comments")
    public Map<String, Object> getComments() {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Comment> data= commentService.getAllComments();
            response.put("data", data);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }

    }

    @PostMapping("/comments/like")
    public Map<String, Object> likeComment(@RequestParam String id) {
        try {
            commentService.incrementLikeCount(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            return response;
        } catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }

    }

    @PostMapping("/insertComment")
    public Map<String, Object> insertComment(@RequestBody CommentDTO evaluate){
        try {
            commentService.insertComment(evaluate);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            return response;
        } catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }
}
